/*
 * my_code.c
 *
 *  Created on: Oct 2, 2021
 *      Author: wuwenfeng
 */
#include "my_code.h"
#include "gpio.h"
#include "button.h"
#include "ht1621.h"
#include "r480r.h"
#include "eeprom.h"
extern ADC_HandleTypeDef hadc;

struct button key1,key2,key3,key4;

config_setting config;


//send data to 959
void Sand_Byte_to_595_2(uint8_t h)
{
	change_io_function(HC595_DLK_GPIO_Port,HC595_DLK_Pin,0);
	HC595_DCK(0);
	HC595_SCK2(0);
	HC595_RCK(0);
	for(char a=0;a<8;a++)
	{
		if((h<<a)&0x80)
		{
			HC595_DCK(1);
		}else
		{
			HC595_DCK(0);
		}
		HC595_SCK2(1);
		HC595_SCK2(0);
	}
	HC595_RCK(1);
	HC595_RCK(0);
}
void HC595_control_server()
{
	char h=0;
	if(dis_buff.led_run==1)
	{
		h|=0x01;
	}
	if(dis_buff.moto1a==1)
	{
		h|=0x02;
	}
	if(dis_buff.moto1b==1)
	{
		h|=0x04;
	}
	if(dis_buff.moto2a==1)
	{
		h|=0x08;
	}
	if(dis_buff.moto2b==1)
	{
		h|=0x10;
	}
	Sand_Byte_to_595_2(h);
}

//motor cool start
void Motoer_control_server()
{
	if(HAL_GetTick()>moto.moto_run)
	{
		moto.moto_run=HAL_GetTick()+10; //set the index follow delay

		if(moto.moto1a>moto.moto1a_)
		{
			moto.moto1a_++;
		}
		if(moto.moto1a<moto.moto1a_)
		{
			moto.moto1a_--;
		}

		if(moto.moto1b>moto.moto1b_)
		{
			moto.moto1b_++;
		}
		if(moto.moto1b<moto.moto1b_)
		{
			moto.moto1b_--;
		}

		if(moto.moto2a>moto.moto2a_)
		{
			moto.moto2a_++;
		}
		if(moto.moto2a<moto.moto2a_)
		{
			moto.moto2a_--;
		}

		if(moto.moto2b>moto.moto2b_)
		{
			moto.moto2b_++;
		}
		if(moto.moto2b<moto.moto2b_)
		{
			moto.moto2b_--;
		}

	}

	moto.pwm_run++;
	if(moto.pwm_run==10)
	{
		moto.pwm_run=0;
	}

	if(moto.pwm_run<moto.moto1a_)
	{
		dis_buff.moto1a=1;
	}else
	{
		dis_buff.moto1a=0;
	}
	if(moto.pwm_run<moto.moto1b_)
	{
		dis_buff.moto1b=1;
	}else
	{
		dis_buff.moto1b=0;
	}
	if(moto.pwm_run<moto.moto2a_)
	{
		dis_buff.moto2a=1;
	}else
	{
		dis_buff.moto2a=0;
	}
	if(moto.pwm_run<moto.moto2b_)
	{
		dis_buff.moto2b=1;
	}else
	{
		dis_buff.moto2b=0;
	}

}

const unsigned char LED_Tab[]=
{
	0x7d,0x05,0x5b,0x1f,0x27,0x3e,0x7e,0x15,0x7f,0x3f,	//0-9
	0x77,0x6e,0x71,0x5e,0x7a,0x73,						//a-f
	0x00,0x02
};
#define A 0x80
#define B 0x40
#define C 0x20
#define D 0x10
#define E 0x08
#define F 0x04
#define G 0x02
#define H 0x01
void HT1621_Display_GetButton(void)
{
	unsigned char send_buff[8];
	unsigned char lcd_buff[4];

	//WritenDataHT1621(0,send_buff,8);

	lcd_buff[0]=LED_Tab[dis_buff.d_num[0]];
	if(dis_buff.dot1==1)
	{
		lcd_buff[0]|=0x80;
	}
	lcd_buff[1]=LED_Tab[dis_buff.d_num[1]];
	if(dis_buff.dot2==1)
	{
		lcd_buff[1]|=0x80;
	}
	lcd_buff[2]=LED_Tab[dis_buff.d_num[2]];
	if(dis_buff.dot3==1)
	{
		lcd_buff[2]|=0x80;
	}
	lcd_buff[3]=LED_Tab[dis_buff.d_num[3]];
	if(dis_buff.dot4==1)
	{
		lcd_buff[3]|=0x80;
	}
	send_buff[0]=lcd_buff[0]>>4;
	send_buff[1]=lcd_buff[0]&0x0f;
	send_buff[2]=lcd_buff[1]>>4;
	send_buff[3]=lcd_buff[1]&0x0f;
	send_buff[4]=lcd_buff[2]>>4;
	send_buff[5]=lcd_buff[2]&0x0f;
	send_buff[6]=lcd_buff[3]>>4;
	send_buff[7]=lcd_buff[3]&0x0f;


	WritenDataHT1621(0,send_buff,8);




}


char check_need_reset()
{
	char a=0;
	change_io_function(MOT_IN1_GPIO_Port, MOT_IN1_Pin,1);
	change_io_function(MOT_IN2_GPIO_Port, MOT_IN2_Pin,0);

	MOT_IN2(0);
	if(READ_MOT_IN1==0)
	{
		MOT_IN2(1);
		if(READ_MOT_IN1==1)
		{
			change_io_function(MOT_IN1_GPIO_Port, MOT_IN1_Pin,0);
			change_io_function(MOT_IN2_GPIO_Port, MOT_IN2_Pin,1);
			MOT_IN1(0);
			if(READ_MOT_IN2==0)
			{
				MOT_IN1(1);
				if(READ_MOT_IN2==1)
				{

					a=1;
				}

			}

		}
	}
	change_io_function(MOT_IN1_GPIO_Port, MOT_IN1_Pin,1);
	change_io_function(MOT_IN2_GPIO_Port, MOT_IN2_Pin,1);
	return a;
}

void my_code()
{
	uint32_t runtime=0,move=0;
	uint8_t mode=1;
	long countdown=1000;
	char moto_sw_buff1,moto_sw_buff2;
	uint16_t moto_sw_buff1_pp=0,moto_sw_buff2_pp=0;

	HT1621_Init();
	r480_init();

	config.begin=0;
	AT24CXX_Read(EEPROM_BEGIN_ADD,(char *)&config,sizeof(config_setting));
	if(config.begin!=CONFIG_EFF_CODE||check_need_reset())
	{
		mode=0;
		r480.key=0;
	}


	while(1)
	{

		//get ADC

		for(char a=0;a<2;a++)
		{
			HAL_ADC_Start(&hadc);
			while(HAL_ADC_PollForConversion(&hadc,0xffff)!=HAL_OK);
			ADCC.adc_filtering[a]+=HAL_ADC_GetValue(&hadc);
		}
		HAL_ADC_Stop(&hadc);
		ADCC.filtering_times+=1;
		if(ADCC.filtering_times==set_filtering_times)
		{
			ADCC.filtering_times=0;
			ADCC.adc_value[0]=ADCC.adc_filtering[0]/set_filtering_times;
			ADCC.adc_filtering[0]=0;
			ADCC.adc_value[1]=ADCC.adc_filtering[1]/set_filtering_times;
			ADCC.adc_filtering[1]=0;
		}


		switch(mode)
		{
		case 0:
			//Startup
			if(HAL_GetTick()>runtime)
			{
				runtime+=1000;

				if(dis_buff.led_run==1)
				{
					dis_buff.led_run=0;
					dis_buff.dot4=1;
					dis_buff.d_num[0]=16;
				}else
				{
					dis_buff.led_run=1;
					dis_buff.dot4=0;
					dis_buff.d_num[0]=10;
				}
			}
			if(r480.key==0xd7)
			{
				config.begin=CONFIG_EFF_CODE;
				config.key_code[0]=r480.add[0];
				config.key_code[1]=r480.add[1];
				config.countdown_set=15000;
				r480.key=0;
				r480.add[0]=0;
				r480.add[1]=0;
				mode=1;
				AT24CXX_Write(EEPROM_BEGIN_ADD,(char *)&config,sizeof(config_setting));

			}
			break;
		case 1:
			//standby
			moto.moto1a=0;
			moto.moto1b=0;
			moto.moto2a=0;
			moto.moto2b=0;
			dis_buff.d_num[0]=16;
			dis_buff.d_num[1]=16;
			dis_buff.d_num[2]=16;
			dis_buff.d_num[3]=16;
			dis_buff.dot1=0;
			dis_buff.dot2=0;
			dis_buff.dot3=0;
			if(HAL_GetTick()>runtime)
			{
				runtime+=1000;

				if(dis_buff.led_run==1)
				{
					dis_buff.led_run=0;
					dis_buff.dot4=1;
				}else
				{
					dis_buff.led_run=1;
					dis_buff.dot4=0;
				}
			}


			if(key2.code!=0)
			{
				mode=2;
				countdown=config.countdown_set;
				moto.moto1a=10;
				moto.moto1b=0;
				moto.moto2a=10;
				moto.moto2b=0;
			}
			if(key3.code!=0)
			{
				moto.moto1a=0;
				moto.moto1b=10;
				moto.moto2a=0;
				moto.moto2b=10;
				mode=3;
				countdown=config.countdown_set;
			}
			if(key1.code!=0)
			{
				mode=4;
				countdown=10000;
			}
			break;
		case 2:

			if(HAL_GetTick()>move)
			{
				move=HAL_GetTick()+100;
				if(dis_buff.led_run==1)
				{
					dis_buff.led_run=0;
				}else
				{
					dis_buff.led_run=1;
				}
				countdown-=100;
				if(countdown<0)
				{
					mode=1;
				}
			}
			dis_buff.d_num[3]=(countdown/100)%10;
			dis_buff.d_num[2]=(countdown/1000)%10;
			dis_buff.d_num[1]=((countdown/10000)%10);
			dis_buff.d_num[1]=dis_buff.d_num[1]==0?16:dis_buff.d_num[1];
			dis_buff.dot4=1;

			if(key3.code!=0)
			{
				mode=3;
				countdown=config.countdown_set-countdown;
				moto.moto1a=0;
				moto.moto1b=10;
				moto.moto2a=0;
				moto.moto2b=10;
			}
			if(key4.code!=0||(moto.moto1a==0&&moto.moto1b==0&&moto.moto2a==0&&moto.moto2b==0))
			{
				mode=1;
			}

			if(ADCC.adc_value[1]>400||(moto_sw_buff1==1&&READ_MOT_IN1==0))
			{
				//overload_times=0;
				moto.moto1a=0;
				moto.moto1b=0;
			}
			if(ADCC.adc_value[0]>400||(moto_sw_buff2==1&&READ_MOT_IN2==0))
			{
				moto.moto2a=0;
				moto.moto2b=0;
			}
			break;
		case 3:

			if(HAL_GetTick()>move)
			{
				move=HAL_GetTick()+100;
				if(dis_buff.led_run==1)
				{
					dis_buff.led_run=0;
				}else
				{
					dis_buff.led_run=1;
				}
				countdown-=100;
				if(countdown<0)
				{
					mode=1;
				}
			}
			dis_buff.d_num[3]=(countdown/100)%10;
			dis_buff.d_num[2]=(countdown/1000)%10;
			dis_buff.d_num[1]=((countdown/10000)%10);
			dis_buff.d_num[1]=dis_buff.d_num[1]==0?16:dis_buff.d_num[1];
			dis_buff.dot4=1;
			if(key2.code!=0)
			{
				mode=2;
				countdown=config.countdown_set-countdown;
				moto.moto1a=10;
				moto.moto1b=0;
				moto.moto2a=10;
				moto.moto2b=0;
			}
			if(key4.code!=0||(moto.moto1a==0&&moto.moto1b==0&&moto.moto2a==0&&moto.moto2b==0))
			{
				mode=1;
			}
			if(ADCC.adc_value[1]>400||(moto_sw_buff1==1&&READ_MOT_IN1==0))
			{
				//overload_times=0;
				moto.moto1a=0;
				moto.moto1b=0;
			}
			if(ADCC.adc_value[0]>400||(moto_sw_buff2==1&&READ_MOT_IN2==0))
			{
				//overload_times2=0;
				moto.moto2a=0;
				moto.moto2b=0;
			}
			break;
		case 4:
			//setting mode
			dis_buff.led_run=0;
			if(HAL_GetTick()>move)
			{
				move=HAL_GetTick()+100;
				if(dis_buff.dot4==1)
				{
					dis_buff.dot4=0;
				}else
				{
					dis_buff.dot4=1;
				}
				countdown-=100;


				if(key2.code<0){config.countdown_set+=1000;countdown=10000;}
				if(key3.code<0){config.countdown_set-=1000;countdown=10000;}

			}

			if(key2.code>0){config.countdown_set+=100;countdown=10000;}
			if(key3.code>0){config.countdown_set-=100;countdown=10000;}
			if(config.countdown_set<100){config.countdown_set=100;}
			if(config.countdown_set>60000){config.countdown_set=60000;}

			if(key4.code!=0||countdown<0)
			{
				mode=1;
				AT24CXX_Write(EEPROM_BEGIN_ADD,(char *)&config,sizeof(config_setting));
			}
			dis_buff.d_num[3]=(config.countdown_set/100)%10;
			dis_buff.d_num[2]=(config.countdown_set/1000)%10;
			dis_buff.d_num[1]=((config.countdown_set/10000)%10);
			dis_buff.d_num[1]=dis_buff.d_num[1]==0?16:dis_buff.d_num[1];


			break;



		}





		if(r480.add[0]==config.key_code[0]&&r480.add[1]==config.key_code[1])
		{
			switch(r480.key)
			{
			case 0xd7:
				dis_buff.button_flag[0]=1;
				break;
			case 0xdb:
				dis_buff.button_flag[1]=1;
				break;
			case 0xdd:
				dis_buff.button_flag[2]=1;
				break;
			case 0xde:
				dis_buff.button_flag[3]=1;
				break;
			}
		}

		r480.key=0;
		r480.add[0]=0;
		r480.add[1]=0;
		GEI_BUTTON_CODE(&key1,dis_buff.button_flag[0]);
		GEI_BUTTON_CODE(&key2,dis_buff.button_flag[1]);
		GEI_BUTTON_CODE(&key3,dis_buff.button_flag[2]);
		GEI_BUTTON_CODE(&key4,dis_buff.button_flag[3]);

		dis_buff.button_flag[0]=0;
		dis_buff.button_flag[1]=0;
		dis_buff.button_flag[2]=0;
		dis_buff.button_flag[3]=0;

		if(moto_sw_buff1!=READ_MOT_IN1)
		{
			moto_sw_buff1_pp++;
			if(moto_sw_buff1_pp==100)
			{
				moto_sw_buff1_pp=0;
				moto_sw_buff1=READ_MOT_IN1;
			}
		}

		if(moto_sw_buff2!=READ_MOT_IN2)
		{
			moto_sw_buff2_pp++;
			if(moto_sw_buff2_pp==100)
			{
				moto_sw_buff2_pp=0;
				moto_sw_buff2=READ_MOT_IN2;
			}
		}

		HT1621_Display_GetButton();
		HC595_control_server();
		Motoer_control_server();
	}
}
